package org.jeecg.modules.zcgl.tencent.config;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.modules.zcgl.tencent.service.IAttentionInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 登陆验证
 * @author wanlf
 */
@Component
@Aspect
public class CheckLoginAop {

    @Resource
    HttpServletRequest request;
    @Autowired
    private IAttentionInfoService attentionInfoService;

    @Pointcut("@annotation(org.jeecg.modules.zcgl.tencent.config.CheckLogin)")
    public void controller(){

    }

    @Around("controller()")
    public Object beforeController(ProceedingJoinPoint joinPoint)throws Throwable{
        String attentionId = JwtUtil.getUserNameByToken(request);
        if (attentionId == null || attentionInfoService.getById(attentionId) == null) {
            throw new JeecgBootException(CommonConstant.SC_JEECG_NO_LOGIN, "未登录或者登录信息错误");
        } else {
            return joinPoint.proceed();
        }
    }

}

